from baseview import BaseView
from ..devicetree import DeviceTree
from ..bases import widgets
from .. import editors

class GroupsView(BaseView):
    topwidget_name = 'Groups'
    topwidget_cls = widgets.HPane
    def __init__(self, **kwargs):
        self.topwidget = widgets.HBox()
        super(GroupsView, self).__init__(**kwargs)
        self.Groups = self.MainController.Groups
        
        self.group_editor = None
        self.group_list = widgets.TreeList(name='Groups', column_names=['id', 'Num', 'Name', 'Submaster'], 
                                           list_types=[str, int, str, bool], column_order=[1, 2, 3], 
                                           default_sort_column=1)
        self.update_group_list()
        self.group_list.connect('selection_changed', self.on_group_list_selection_changed)
        
        btnbox = widgets.HBox()
        self.cbSubmaster = widgets.CheckBox(label='Submaster Mode')
        btnbox.pack_start(self.cbSubmaster)
        for key in ['Add', 'Delete']:
            btn = widgets.Button(label=key)
            btnbox.pack_start(btn)
            btn.connect('clicked', self.on_btn_add_delete_clicked, key)
        self.group_list.topwidget.pack_start(btnbox)
        
        self.topcontainer.pack_start(self.group_list.topwidget, expand=True)
        self.topwidget.show_all()
        
        self.Groups.bind(child_added=self.on_groups_child_added, 
                         child_removed=self.on_groups_child_removed)
    
    def unlink(self):
        super(GroupsView, self).unlink()
        self.Groups.unbind(self.on_groups_child_added, self.on_groups_child_removed)
        for group in self.MainController.Groups.itervalues():
            group.disconnect(callback=self.on_group_property_changed)
    
    def update_group_list(self, key=None):
        if key:
            groups = {key:self.Groups[key]}
        else:
            groups = self.Groups
        for key, val in groups.iteritems():
            if 'MainController_' not in key:
                i = val.Index
                if i is None:
                    i = -1
                self.group_list.update({key:[i, val.name, val.submaster]})
                val.connect('property_changed', self.on_group_property_changed)
            
    def set_current_group(self, group):
        if self.group_editor:
            self.remove_editor(self.group_editor)
        self.add_editor(editors.GroupEditor, 'group_editor', group=group)
        self.topcontainer.pack_start(self.group_editor.topwidget, expand=True)
        
    def on_group_list_selection_changed(self, **kwargs):
        key = kwargs.get('key')
        if key:
            group = self.Groups[key]
            self.set_current_group(group)
        
    def on_groups_child_added(self, **kwargs):
        obj = kwargs.get('obj')
        self.update_group_list(obj.id)
        
    def on_groups_child_removed(self, **kwargs):
        obj = kwargs.get('obj')
        self.group_list.clear()
        self.update_group_list()
        
    def on_btn_add_delete_clicked(self, btn, key):
        if key == 'Add':
            group = self.MainController.add_group(name='New Group', submaster=self.cbSubmaster.state)
            group.name = 'New Group %s' % (group.Index)
            self.update_group_list(group.id)
            self.group_list.set_current_selection(key=group.id)
            self.set_current_group(group)
        elif key == 'Delete' and self.group_list.current_selection:
            self.MainController.del_group(id=self.group_list.current_selection)
            #self.group_list.clear()
            #self.update_group_list()
            
    def on_group_property_changed(self, **kwargs):
        obj = kwargs.get('obj')
        self.update_group_list(obj.id)
        
